#!/usr/bin/python

"""
    File:       Log.py
    Brief:      output package declare
    Author:     Xu Qiang
    Version:    1.0
    Date:       2017 - 4 - 18    
"""


import sys
import datetime
import time
from datetime import datetime


def getFormatedTimeString():
    try:
        timeNow = datetime.now()
        timestring = timeNow.strftime( "%Y-%m-%d %H:%M:%S." )
        millisecond = str( timeNow.microsecond / 1000 )
        millisecondstring = '0' * ( 3 - len( millisecond ) ) + millisecond
        timestring = timestring + millisecondstring
        return timestring
    except:
        return "****-**-** **:**:**.***"

def writeLog( fileStream, level, info ):
    timestring = getFormatedTimeString()
    try:
        if fileStream == None:
            pass
        else:
            fileStream.write( timestring + " " + level + " " + info.replace( '\n', '\\n' ) + "\n" )
            fileStream.flush()
    except Exception, e:
        print "wirteLog failed by %s" % str( e )

class Log:
    fileStreamList = []
    @staticmethod
    def write( level, info ):
        for stream in Log.fileStreamList:
            writeLog( stream, level, info )

    @staticmethod
    def addFileStream( fileStream ):
        Log.fileStreamList.append( fileStream )

    @staticmethod
    def removeFileStream( fileStream ):
        Log.fileStreamList.remove( fileStream )

class LogI( Log ):
    fileStreamList = [sys.stdout]
    @staticmethod
    def write( info ):
        Log.write( "-I-", info )
        for stream in LogI.fileStreamList:
            writeLog( stream, "-I-", info )

    @staticmethod
    def addFileStream( fileStream ):
        LogI.fileStreamList.append( fileStream )

    @staticmethod
    def removeFileStream( fileStream ):
        LogI.fileStreamList.remove( fileStream )

class LogD( Log ):
    fileStreamList = []
    @staticmethod
    def write( info ):
        Log.write( "-D-", info )
        for stream in LogD.fileStreamList:
            writeLog( stream, "-D-", info )

    @staticmethod
    def addFileStream( fileStream ):
        LogD.fileStreamList.append( fileStream )

    @staticmethod
    def removeFileStream( fileStream ):
        LogD.fileStreamList.remove( fileStream )

class LogE( Log ):
    fileStreamList = [sys.stderr]
    @staticmethod
    def write( info ):
        Log.write( "-E-", info )
        for stream in LogE.fileStreamList:
            writeLog( stream, "-E-", info )

    @staticmethod
    def addFileStream( fileStream ):
        LogE.fileStreamList.append( fileStream )

    @staticmethod
    def removeFileStream( fileStream ):
        LogE.fileStreamList.remove( fileStream )

class LogW( Log ):
    fileStreamList = [sys.stderr]
    @staticmethod
    def write( info ):
        Log.write( "-W-", info )
        for stream in LogW.fileStreamList:
            writeLog( stream, "-W-", info )

    @staticmethod
    def addFileStream( fileStream ):
        LogW.fileStreamList.append( fileStream )

    @staticmethod
    def removeFileStream( fileStream ):
        LogW.fileStreamList.remove( fileStream )

from Static import logFile, logIFile, logDFile, logEFile, logWFile
Log.addFileStream( open( logFile , "a+" ) )
LogI.addFileStream( open( logIFile, "a+" ) )
LogD.addFileStream( open( logDFile, "a+" ) )
LogE.addFileStream( open( logEFile, "a+" ) )
LogW.addFileStream( open( logWFile, "a+" ) )
